package com.example.cadmin.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cadmin.entity.MyMenu;
import com.example.cadmin.entityVo.MenuIndexDto;
import com.example.cadmin.mapper.MyMenuMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zzsoft
 * @since 2023-01-07
 */
@Service
public class MyMenuService extends ServiceImpl<MyMenuMapper, MyMenu> {
    @Resource
    private MyMenuMapper myMenuMapper;

    public static MenuIndexDto recursiveTree(MenuIndexDto parent, List<MenuIndexDto> myMenuVoList) {
        List<MenuIndexDto>children = new ArrayList<>();
        for (MenuIndexDto menu : myMenuVoList) {
            if (Objects.equals(parent.getId(), menu.getParentId())) {
                children.add(menu);
            }
            parent.setChildren(children);
        }
        return parent;
    }

    public List<MenuIndexDto> getMenu(Integer userId){
        // 数据库取出全部数据
        List<MenuIndexDto> myMenuVoList =  myMenuMapper.getMenu(userId);
        List<MenuIndexDto> result = new ArrayList<MenuIndexDto>();
        //获取一级节点菜单
        for (MenuIndexDto myMenuVo:myMenuVoList){
            if (myMenuVo.getParentId() == 0){
                result.add(myMenuVo);
            }
        }
        //获取二级节点菜单
        // 2、递归获取子节点
        for (MenuIndexDto parent : result) {
            recursiveTree(parent, myMenuVoList);
        }
        return result;
    }
}
